package org.jeecg.modules.demo.yxclosingdate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.advancesReceived.entity.YxAdvancesReceived;
import org.jeecg.modules.demo.allout.entity.YxAllot;
import org.jeecg.modules.demo.caiWuSkd.entity.CwSkd;
import org.jeecg.modules.demo.caiWuSrOrder.entity.CwSrOrder;
import org.jeecg.modules.demo.checkOrder.entity.checkOrder;
import org.jeecg.modules.demo.cwSaleInvoice.entity.CwSaleInvoice;
import org.jeecg.modules.demo.puInvoice.entity.PuInvoice;
import org.jeecg.modules.demo.pubuyorder.entity.PuBuyOrder;
import org.jeecg.modules.demo.pusettlement.entity.PuSettlement;
import org.jeecg.modules.demo.qualityOrder.entity.PuQualityOrder;
import org.jeecg.modules.demo.soFeiyong.entity.SoFeiyong;
import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soQualityOrder.entity.SoQualityOrder;
import org.jeecg.modules.demo.soSaleOrder.entity.SoSaleOrder;
import org.jeecg.modules.demo.soSettlement.entity.SoSettlement;
import org.jeecg.modules.demo.socost.entity.SoCost;
import org.jeecg.modules.demo.sopayorder.entity.SoPayorder;
import org.jeecg.modules.demo.utils.DateUtils;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.yxadjust.entity.YxAdjust;
import org.jeecg.modules.demo.yxclosingdate.entity.YxClosingDate;
import org.jeecg.modules.demo.yxclosingdate.service.IYxClosingDateService;
import org.jeecg.modules.demo.yxclosingorder.entity.YxClosingOrder;
import org.jeecg.modules.demo.yxkcqcorder.entity.YxKcqcOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: yx_closing_date
 * @Author: jeecg-boot
 * @Date:   2022-11-15
 * @Version: V1.0
 */
@Api(tags="yx_closing_date")
@RestController
@RequestMapping("/yxclosingdate/yxClosingDate")
@Slf4j
public class YxClosingDateController extends JeecgController<YxClosingDate, IYxClosingDateService> {
	@Autowired
	private IYxClosingDateService yxClosingDateService;

	@Value(value = "${projectDate}")
	private String projectDate;
	
	/**
	 * 分页列表查询
	 *
	 * @param yxClosingDate
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "月末结账-分页列表查询")
	@ApiOperation(value="月末结账-分页列表查询", notes="月末结账-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(YxClosingDate yxClosingDate,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxClosingDate> queryWrapper = QueryGenerator.initQueryWrapper(yxClosingDate, req.getParameterMap());
		Page<YxClosingDate> page = new Page<YxClosingDate>(pageNo, pageSize);
		IPage<YxClosingDate> pageList = yxClosingDateService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	 @AutoLog(value = "月末结账-分页列表查询")
	 @ApiOperation(value="月末结账-分页列表查询", notes="月末结账-分页列表查询")
	 @GetMapping(value = "/getList")
	 public String getList(@RequestParam(name="date",required=true)String date) {
		 Gson gson = new Gson();
		 Map map = new HashMap();
		 try{
			 List<Map<String,String>> list = yxClosingDateService.getList(date);
			 map.put("code",200);
			 map.put("message","成功！");
			 map.put("data",list);
			 String json = gson.toJson(map);
			 return json;
		 }catch (Exception e){
		 	e.printStackTrace();
			 map.put("code",500);
			 map.put("message","系统错误！");
			 String json = gson.toJson(map);
			 return json;
		 }

	 }

	 @PostMapping(value = "/getJzByDate")
	 public String getJzByDate(@RequestParam(name="date",required=true)String date){
		 Gson gson = new Gson();
		 Map map = new HashMap();
		try{
			map.put("code",200);
			map.put("message","查询成功！");
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
			YxClosingDate yxClosingDate = new YxClosingDate();
			yxClosingDate.setJzTime(dateFormat.parse(date));
			Integer  cnt = yxClosingDateService.getJzByDate(yxClosingDate);
			String time = yxClosingDateService.getNewstDate(yxClosingDate);
			if(time ==null){
				time = projectDate;
				String subtime= time.substring(0,7).replace("-","");
				StringBuffer stringBuilder1=new StringBuffer(subtime);
				stringBuilder1.insert(4,"-");
				map.put("newDate",stringBuilder1);
			}else{
				String subtime= time.substring(0,7).replace("-","");
				String newDate = DateUtils.getPreMonth(subtime);
				StringBuffer stringBuilder1=new StringBuffer(newDate);
				stringBuilder1.insert(4,"-");
				map.put("newDate",stringBuilder1);
			}
			map.put("monthClosing",cnt>0?1:0);
			map.put("projectDate",projectDate);
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	 }


	 /**
	 *   添加
	 *
	 * @param yxClosingDate
	 * @return
	 */
	@AutoLog(value = "月末结账-添加")
	@ApiOperation(value="月末结账-添加", notes="月末结账-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxClosingDate yxClosingDate) {
		yxClosingDateService.save(yxClosingDate);
		return Result.OK("添加成功！");
	}


	 /**
	  *   月末结账
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "月末结账-月末结账")
	 @ApiOperation(value="月末结账-月末结账", notes="月末结账-月末结账")
	 @PostMapping(value = "/monthEndClosing")
	 public Result<?> monthEndClosing(@RequestParam(name="str",required=true)String str) {
	 	try{
	 		YxClosingDate yxClosingDate = new YxClosingDate();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
			yxClosingDate.setJzTime(dateFormat.parse(str));
			yxClosingDate.setCreateTime(new Date());
			List<PuBuyOrder> puBuyOrders = yxClosingDateService.getPuBuyOrder(yxClosingDate);
			List<PuQualityOrder> puQualityOrders = yxClosingDateService.getPuQualityOrder(yxClosingDate);
			List<WarehouseOrder>  warehouseOrders = yxClosingDateService.getWarehouseOrder(yxClosingDate);
			List<PuSettlement> puSettlements = yxClosingDateService.getPuSettlement(yxClosingDate);
			List<PuInvoice> puInvoices=yxClosingDateService. getPuInvoice(yxClosingDate);
			List<SoPayorder> soPayorders = yxClosingDateService. getSoPayOrder(yxClosingDate);
			List<SoCost> soCosts = yxClosingDateService.getSoCost(yxClosingDate);
			List<YxAdvancesReceived>  yxAdvancesReceiveds = yxClosingDateService.getYfk(yxClosingDate);
			List<checkOrder> checkOrders = yxClosingDateService.getPd(yxClosingDate);
			List<YxAdjust> adjusts=yxClosingDateService.getAdjust(yxClosingDate);
			List<YxAllot> allots = yxClosingDateService.getAllot(yxClosingDate);
			List<YxKcqcOrder> kcqcOrders = yxClosingDateService. getQcOrder(yxClosingDate);

			List<SoSaleOrder> so = yxClosingDateService.getSoorder(yxClosingDate);
			List<SoOrderDetail> sod = yxClosingDateService.getSod(yxClosingDate);
			List<SoQualityOrder> soq = yxClosingDateService.getSoq(yxClosingDate);
			List<SoSettlement> sos = yxClosingDateService.getSos(yxClosingDate);
			List<CwSaleInvoice > cw = yxClosingDateService.getSocw(yxClosingDate);
			List<CwSkd> sk = yxClosingDateService.getSosk(yxClosingDate);
			List<CwSkd> ysk = yxClosingDateService.getSoysk(yxClosingDate);
			List<SoFeiyong> sofy = yxClosingDateService.getSofy(yxClosingDate);
			List<CwSrOrder> sosr = yxClosingDateService.getSosr(yxClosingDate);

			if(!puBuyOrders.isEmpty()){
				return Result.error(300,"采购订单有未审核单据，结账失败！");
			}
			if(!puQualityOrders.isEmpty()){
				return Result.error(300,"采购质检单有未审核单据，结账失败！");
			}
			if(!warehouseOrders.isEmpty()){
				return Result.error(300,"采购入库单/其他入库单有未审核单据，结账失败！");
			}
			if(!puSettlements.isEmpty()){
				return Result.error(300,"采购结算单有未审核单据，结账失败！");
			}
			if(!puInvoices.isEmpty()){
				return Result.error(300,"采购发票单有未审核单据，结账失败！");
			}
			if(!soPayorders.isEmpty()){
				return Result.error(300,"普通付款单有未审核单据，结账失败！");
			}
			if(!soCosts.isEmpty()){
				return Result.error(300,"采购费用单有未审核单据，结账失败！");
			}
			if(!yxAdvancesReceiveds.isEmpty()){
				return Result.error(300,"预付款单有未审核单据，结账失败！");
			}
			if(!checkOrders.isEmpty()){
				return Result.error(300,"盘点单有未审核单据，结账失败！");
			}
			if(!adjusts.isEmpty()){
				return Result.error(300,"成本调整单有未审核单据，结账失败！");
			}
			if(!allots.isEmpty()){
				return Result.error(300,"调拨单有未审核单据，结账失败！");
			}
			if(!kcqcOrders.isEmpty()){
				return Result.error(300,"库存期初余额单有未审核单据，结账失败！");
			}


			if(!so.isEmpty()){
				return Result.error(300,"销售订单有未审核单据，结账失败！");
			}
			if(!sod.isEmpty()){
				return Result.error(300,"销售出库单有未审核单据，结账失败！");
			}
			if(!soq.isEmpty()){
				return Result.error(300,"销售质检单有未审核单据，结账失败！");
			}
			if(!sos.isEmpty()){
				return Result.error(300,"销售结算单有未审核单据，结账失败！");
			}
			if(!cw.isEmpty()){
				return Result.error(300,"销售发票单有未审核单据，结账失败！");
			}
			if(!sk.isEmpty()){
				return Result.error(300,"收款单有未审核单据，结账失败！");
			}
			if(!ysk.isEmpty()){
				return Result.error(300,"预收款单有未审核单据，结账失败！");
			}
			if(!sofy.isEmpty()){
				return Result.error(300,"销售费用单有未审核单据，结账失败！");
			}
			if(!sosr.isEmpty()){
				return Result.error(300,"收入单有未审核单据，结账失败！");
			}

			if(yxClosingDateService.monthEndClosing(yxClosingDate)){
				return Result.OK("结账成功！");
			}
			return Result.error(500,"结账失败！");
		}catch (Exception e){
	 		e.printStackTrace();
	 		return Result.error(500,"系统错误！");
		}
	 }

	 @PostMapping(value = "/getAssociatedQueryByWoNo")
	 public String getAssociatedQueryByWoNo(@RequestParam(name="str",required=true)String str){
		 Gson gson = new Gson();
		 Map map = new HashMap();
	 	try{
	 		List<YxClosingOrder> redOrderList = yxClosingDateService.getAssociatedQueryByWoNo(str);
			map.put("code",200);
			map.put("redOrderList",redOrderList);
			map.put("message","查询成功！");
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
	 		e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	 }

	 /**
	  *   取消结账
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "月末结账-取消结账")
	 @ApiOperation(value="月末结账-取消结账", notes="月末结账-取消结账")
	 @PostMapping(value = "/cancelMonthEndClosing")
	 public Result<?> cancelMonthEndClosing(@RequestParam(name="str",required=true)String str) {
		 try{
			 YxClosingDate yxClosingDate = yxClosingDateService.getByDate(str);
			 if(yxClosingDateService.cancelMonthEndClosing(yxClosingDate)){
				 return Result.OK("取消成功！");
			 }
			 return Result.error(500,"取消失败！");
		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }
	 }
	
	/**
	 *  编辑
	 *
	 * @param yxClosingDate
	 * @return
	 */
	@AutoLog(value = "月末结账-编辑")
	@ApiOperation(value="月末结账-编辑", notes="月末结账-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxClosingDate yxClosingDate) {
		yxClosingDateService.updateById(yxClosingDate);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "月末结账-通过id删除")
	@ApiOperation(value="月末结账-通过id删除", notes="月末结账-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		yxClosingDateService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "yx_closing_date-批量删除")
	@ApiOperation(value="yx_closing_date-批量删除", notes="yx_closing_date-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxClosingDateService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "yx_closing_date-通过id查询")
	@ApiOperation(value="yx_closing_date-通过id查询", notes="yx_closing_date-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxClosingDate yxClosingDate = yxClosingDateService.getById(id);
		if(yxClosingDate==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxClosingDate);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxClosingDate
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxClosingDate yxClosingDate) {
        return super.exportXls(request, yxClosingDate, YxClosingDate.class, "yx_closing_date");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxClosingDate.class);
    }

}
